package com.wandian.news.mvvm.challenge;

import android.app.Application;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.JsonObject;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.news.bean.ChallengeBean;
import com.wandian.news.bean.ChallengePDTBean;
import com.wandian.news.bean.ChallengeXZTBean;
import com.wandian.news.data.BaseAppApi;

import java.util.HashMap;
import java.util.Random;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_WORK_PD;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_WORK_WD;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description ChallengeViewModel
 **/
public class ChallengeViewModel extends BaseViewModel {
    private TTBaseAppApi baseTTApi;
    private BaseAppApi baseAppApi;
    public String url = NEWS_KNOWLEDGE_WORK_WD;//请求链接
    public MutableLiveData<ChallengeXZTBean.NewslistEntity> dataXZTBean =  new MutableLiveData<>();
    public MutableLiveData<ChallengePDTBean.NewslistEntity> dataPDTBean =  new MutableLiveData<>();
    public MutableLiveData<ChallengeBean> cBean =  new MutableLiveData<>();
    public ChallengeViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
        baseTTApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }

    public void getData(){
        Random random  = new Random();
        if(random.nextInt(25)>5){
            url = NEWS_KNOWLEDGE_WORK_WD;
        }else {
            url = NEWS_KNOWLEDGE_WORK_PD;
        }
        getUrlData(url);
    }


    public void getUrlData(String urlApi){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseTTApi.getTouTiaoList(urlApi,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            switch (url){
                                case NEWS_KNOWLEDGE_WORK_WD:
                                    ChallengeXZTBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeXZTBean.class);
                                    if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                        dataXZTBean.setValue(bean.getNewslist().get(0));
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_WORK_PD:
                                    ChallengePDTBean bean_pd = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengePDTBean.class);
                                    if(!UIUtils.isEmpty(bean_pd) && bean_pd.getCode()==200 && !UIUtils.isEmpty(bean_pd.getNewslist()) && bean_pd.getNewslist().size()>0){
                                        dataPDTBean.setValue(bean_pd.getNewslist().get(0));
                                    }
                                    break;
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    /**
     * 更新战力
     */
    public void notifyUserZL(int status){

        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("status", status+"");
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.upFightNum(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("upFightNum",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            ChallengeBean challengeBean = (ChallengeBean)JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),ChallengeBean.class);
                            if(!UIUtils.isEmpty(challengeBean)){
                                cBean.setValue(challengeBean);
                            }else {
                                ToastUtil.show("账号异常");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
